package com.pro.xwt.manager.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.service.BEmployeeService;
import com.pro.xwt.common.utils.Common;

@Controller
@RequestMapping("/members")
public class MemberController {
	@Autowired
	BEmployeeService bEmployeeService;

	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> parameter = new HashMap<String, Object>();

		parameter.put("begin", 0);
		parameter.put("size", 10);
		List<BEmployee> memberList = bEmployeeService.queryListAll(parameter);
		model.addAttribute("memberList", memberList);
		int empSum = bEmployeeService.count(parameter);
		int pageSum = empSum / 10;
		if (empSum % 10 == 0) {

		} else {
			pageSum += 1;
		}
		model.addAttribute("pageNo", 1);
		model.addAttribute("pageSum", pageSum);
		return "manager/member/list";

	}

	/**
	 * 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("pageNo") int pageNo) throws Exception {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.clear();
		parameter.put("begin", pageNo * 10);
		parameter.put("size", 10);
		List<BEmployee> memberList = bEmployeeService.queryListAll(parameter);
		model.addAttribute("memberList", memberList);
		int empSum = bEmployeeService.count(parameter);

		int pageSum = empSum / 10;
		if (empSum % 10 == 0) {

		} else {
			pageSum += 1;
		}
		parameter.clear();
		parameter.put("memberList", memberList);
		parameter.put("pageSum", pageSum);
		parameter.put("pageNo", pageNo);
		return parameter;

	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String insert(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		return "manager/member/add";

	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			BEmployee bEmployee) throws Exception {
		BEmployee be = (BEmployee) session.getAttribute("admin");
		Date now = new Date();
		bEmployee.setId(Common.generateUUID());
		bEmployee.setCreateTime(now);
		bEmployee.setDeleteState("0");
		bEmployee.setCreateEmpId(be.getId());
		int tag = bEmployeeService.insert(bEmployee);
		model.addAttribute("subTag", tag);
		return "redirect:/members/list";
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody int update(Model model, HttpSession session, HttpServletRequest request,
			HttpServletResponse response, BEmployee bEmployee) throws Exception {
		int tag = bEmployeeService.update(bEmployee);
		return tag;

	}

	@RequestMapping(value = "/updateEmp", method = RequestMethod.POST)
	public String update1(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			BEmployee bEmployee) throws Exception {
		int tag = bEmployeeService.update(bEmployee);
		if (tag == 0) {
			tag = 2;
		}
		model.addAttribute("upTag", tag);

		return "forward:/members/list";

	}

	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("Id") String id) throws Exception {
		BEmployee be = bEmployeeService.findById(id);
		model.addAttribute("bEmployee", be);
		return "manager/member/update";

	}

}
